package com.atguigu.yygh.hosp.controller.admin;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;

import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-05-18
 */
@RestController
@Api(tags = "设置医院信息")
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {


    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "查询所有的医院信息")
    @GetMapping(value = "/findAll")
    public R findAll() {
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("item", list);
    }

    /**
     * 查询信息 分页
     * @param pageNum
     * @param size
     * @param hospitalSetQueryVo
     * @return
     */
    @PostMapping("page/{pageNum}/{size}")
    public R getPageInfo(@PathVariable("pageNum") Integer pageNum,
                         @PathVariable("size") Integer size,
                         @RequestBody HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> page = new Page<>(pageNum,size);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(hospitalSetQueryVo.getHosname())){
            queryWrapper.like("hosname", hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode())){
            queryWrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
        }
        hospitalSetService.page(page, queryWrapper);
        return R.ok().data("total", page.getTotal()).data("rows", page.getRecords());
    }

    //根据医院设置id删除医院设置信息
    @ApiOperation(value = "删除医院信息")
    @DeleteMapping(value = "/deleteById/{id}")
    public R deleteById(@PathVariable Integer id) {
        hospitalSetService.removeById(id);
        return R.ok();
    }


    @ApiOperation(value = "新增接口")
    @PostMapping("/save")
    public R save(@RequestBody HospitalSet hospitalSet) {
        //状态 0 状态 1使用
        hospitalSet.setStatus(1);
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt()));
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }

    @ApiOperation(value = "修改之前回显")
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable Integer id){
        return R.ok().data("item", hospitalSetService.getById(id));
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public R detail(@RequestBody HospitalSet hospitalSet) {
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    //批量删除
    @DeleteMapping("/delete")
    public R batchDelete(@RequestBody List<Integer> ids){
        hospitalSetService.removeByIds(ids);
        return R.ok();
    }

    //锁定与解锁
    @PutMapping("/updateStatus/{id}/{status}")
    public R updateStatus (@PathVariable Long id,@PathVariable Integer status){
        HospitalSet hospitalSet = new HospitalSet();
        hospitalSet.setId(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

}

